package xml;

import javax.xml.bind.annotation.adapters.*;
import cuShapes.*;
import java.util.*;

public class ShapeCollectionAdapter extends XmlAdapter<ShapeListType, ShapeList> {
	// adapt original Java construct to a type, NotificationsType,
	// which we can easily map to the XML output we want
	public ShapeListType marshal(ShapeList shapes) throws Exception {
		LinkedList<Box> boxes = new LinkedList<Box>();
		LinkedList<Ellipse> ellipses = new LinkedList<Ellipse>();

		for (PQShape e : shapes) {
			if (e instanceof Box){
				boxes.add((Box) e);
			} else if (e instanceof Ellipse) {
				ellipses.add((Ellipse) e);
			}
		}
		return new ShapeListType(boxes, ellipses);
	}

	// reverse operation: map XML type to Java
	public ShapeList unmarshal(ShapeListType shapeList) throws Exception { 
		ShapeList shapes = new ShapeList();
		for(PQShape x : shapeList.getBoxes()) {
			shapes.add((Box) x);
		}
		for(PQShape x : shapeList.getEllipses()) {
			shapes.add((Ellipse) x);
		}
        //shapes.addAll(shapeList.getBoxes());
        //shapes.addAll(shapeList.getEllipses());
        
        return shapes;
	}
}
